package com.yunsc.order.sevice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.common.exception.YunscException;
import com.yunsc.common.utils.PageResult;
import com.yunsc.order.dao.YunscRechargePlanDao;
import com.yunsc.order.dto.RechargePlanDTO;
import com.yunsc.order.entity.YunscRechargePlanEntity;
import com.yunsc.order.sevice.YunscRechargePlanService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;




@Service("yunscRechargePlanService")
public class YunscRechargePlanServiceImpl extends ServiceImpl<YunscRechargePlanDao, YunscRechargePlanEntity> implements YunscRechargePlanService {


    @Override
    public PageResult<YunscRechargePlanEntity> queryRechargePlanList(RechargePlanDTO param, Integer storeId) {
        LambdaQueryWrapper<YunscRechargePlanEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YunscRechargePlanEntity::getStoreId, storeId);
        lqw.like(StringUtils.isNotEmpty(param.getSearch()), YunscRechargePlanEntity::getPlanName, param.getSearch());
        lqw.orderByAsc(YunscRechargePlanEntity::getSort);
        Page<YunscRechargePlanEntity> planEntityPage = this.page(new Page<>(param.getPage(), param.getPageSize()), lqw);

        return PageResult.formatPageResult(planEntityPage.getRecords(), param.getPage(), param.getPageSize(), planEntityPage.getTotal());
    }

    @Override
    public void addRechargePlan(YunscRechargePlanEntity plan, Integer storeId) {
        plan.setStoreId(storeId);
        this.save(plan);
    }

    @Override
    public void editRechargePlan(RechargePlanDTO param) {
        YunscRechargePlanEntity plan = this.getById(param.getPlanId());
        if (plan == null){
            throw new YunscException("充值计划不存在");
        }
        BeanUtils.copyProperties(param.getForm(), plan);
        plan.setPlanId(param.getPlanId());

        this.updateById(plan);

    }

    @Override
    public void deleteRechargePlan(Integer planId) {
        this.removeById(planId);
    }
}